package org.ellisande.grid.tags;

import java.util.EnumSet;

import javax.servlet.jsp.JspWriter;

import org.ellisande.grid.tags.support.TagBuilder;
import org.ellisande.tld.annotations.Attribute;
import org.ellisande.tld.annotations.Description;
import org.ellisande.tld.annotations.Info;
import org.ellisande.tld.annotations.Tag;

@Tag
@Info("Form element. Allows for different styles and form types.")
public class FormTag extends AbstractTag {

	private static final long serialVersionUID = 552343850322501924L;

	private static enum Orientation {
		VERTICAL(""), HORIZONTAL("form-horizontal"), SEARCH("form-search");

		private String orientation;

		private Orientation(String orient) {
			this.orientation = orient;
		}

		public String style() {
			return orientation;
		}
	}

	private static enum Type {
		DEFAULT(""), INLINE("form-inline"), SEARCH("form-search");

		private String type;

		private Type(String type) {
			this.type = type;
		}

		public String style() {
			return type;
		}
	}

	@Attribute
	@Description("Defines the orientation of the form. Vertical stacks fields and labels. Horizontal stacks form elements, but put labels on the same line. Allowed values: vertical | horizontal. Default value: vertical.")
	private Orientation orientation = Orientation.VERTICAL;

	@Attribute
	@Description("Defines the type of this form element. Allowed values: default | inline | search. Default value: default.")
	private Type type = Type.DEFAULT;

	@Attribute
	@Description("Defines whether or not this form area should be inset. Allowed values: true | false. Default value: false.")
	private boolean inset = false;
	
	@Attribute
	@Description("Defines the action to be taken when the form is submitted")
	private String action = "";
	
	@Attribute
	@Description("Defines the method of that should be used while sumbitting the form. Allowed values: post|get")
	private String method = "post";

	private static final String INSET_STYLE = "well";

	@Override
	public void startTag() throws Exception {
		TagBuilder form = createTagBuilder("form");
		form.addStyle(orientation.style());
		form.addStyle(type.style());
		form.addAttribute("action", action);
		form.addAttribute("method", method);
		if (inset) {
			form.addStyle(INSET_STYLE);
		}

		JspWriter out = pageContext.getOut();
		out.write(form.compileStartTag());

	}

	@Override
	public void endTag() throws Exception {
		JspWriter out = pageContext.getOut();
		out.write("</form>");

	}

	@Override
	public void release() {
		super.release();
		inset = false;
		type = null;
		orientation = null;
	}

	public void setType(String newType) {
		for (Type type : EnumSet.allOf(Type.class)) {
			if (type.name().equalsIgnoreCase(newType)) {
				this.type = type;
				return;
			}
		}
	}

	public void setOrientation(String newOrient) {
		for (Orientation orient : EnumSet.allOf(Orientation.class)) {
			if (orient.name().equalsIgnoreCase(newOrient)) {
				this.orientation = orient;
				return;
			}
		}
	}

	public void setAction(String action) {
		this.action = action;
	}
}
